<?php

namespace app\store\model\presalesCustomer;

class Log extends \app\common\model\presalesCustomer\Log
{
    /**
     * 检索查询条件
     * @param array $param
     * @return array
     */
    private function getFilter(array $param = []): array
    {
        // 默认查询条件
        $param = $this->setQueryDefaultValue($param, [
            'search' => '',
            'pc_id' => '',
            'status' => -1,
            'create_time' => [],

        ]);
        // 检索查询条件
        $filter = [];
        !empty($param['search']) && $filter[] = ['type_name', 'like', "%{$param['search']}%"];
        !empty($param['pc_id']) && $filter[] = ['pc_id', '=', $param['pc_id']];
        $param['status'] > -1 && $filter[] = ['status', '=', $param['status']];

        if (!empty($param['create_time'])) {
            $times = between_time($param['create_time']);
            $filter[] = ['create_time', '>=', $times['start_time']];
            $filter[] = ['create_time', '<', $times['end_time'] + 86400];
        }


        return $filter;
    }

    /**
     * 获取列表记录
     * @param array $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param = [], array $store = []): \think\Paginator
    {
        // 检索查询条件
        $filter = $this->getFilter($param);

        if (empty($param['pc_id']) && $store['user']['is_super'] != 1) {
            $filter[] = ['store_user_id', '=', $store['user']['store_user_id']];
        }

        // 查询列表数据
        return $this->where($filter)
            ->with(['store_user', 'type'])
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate(15);
    }

    public function all(array $param = [], array $store = [])
    {
        // 检索查询条件
        $filter = $this->getFilter($param);

        // 查询列表数据
        return $this->where($filter)
            ->where('is_delete', '=', 0)
            ->where('status', '=', 0)
            ->order(['create_time' => 'desc'])
            ->select();
    }

    public function createData($data)
    {
        if (!empty($data['next_time'])) {
            $data['next_time'] = strtotime($data['next_time']);
        }
        return $data;
    }

    /**
     * 添加新记录
     * @param array $data
     * @return bool|false
     */
    public function adds(array $data, array $store = []): bool
    {
        $data['store_id'] = self::$storeId;
        $data['store_user_id'] = $store['user']['store_user_id'];

        if (!empty($data['pc_id']) && !empty($data['pct_id'])) {
            $pc = (new PresalesCustomer())->details($data['pc_id']);
            if ($pc) {
                $pc->pct_id = $data['pct_id'];
                $pc->next_log_time = $this->createData($data)['next_time'] ?? 0;
                $pc->last_log_time = time();
                $pc->save();
            }
        }

        return $this->save($this->createData($data)) !== false;
    }

    public function getExpireLog($store)
    {
        $currTime = time();

        // 当前无需跟进的类型id
        $pct_id = (new Type())->where(['not_log' => 1, 'is_delete' => 0])->column('pct_id');

        if ($store['user']['is_super'] == 1) {
            //超级管理员查看全部
            $where1 = [
                ['pct_id', 'not in', $pct_id],
                ['next_log_time', '>', time()],
                ['next_log_time', '<', strtotime(date("Y-m-d 23:59:59"))],
                ['is_delete', '=', 0],
            ];
            $where2 = [
                ['pct_id', 'not in', $pct_id],
                ['next_log_time', '<', time()],
                ['next_log_time', '<>', 0],
                ['is_delete', '=', 0],
            ];
        } else {
            $where1 = [
                ['pct_id', 'not in', $pct_id],
                ['next_log_time', '>', time()],
                ['next_log_time', '<', strtotime(date("Y-m-d 23:59:59"))],
                ['is_delete', '=', 0],
                ['store_user_id', '=', $store['user']['store_user_id']]
            ];
            $where2 = [
                ['pct_id', 'not in', $pct_id],
                ['next_log_time', '<', time()],
                ['next_log_time', '<>', 0],
                ['is_delete', '=', 0],
                ['store_user_id', '=', $store['user']['store_user_id']],
            ];
        }

        //今日待跟进数量
        $today_count = (new \app\common\model\presalesCustomer\PresalesCustomer())
            ->where($where1)->count();
        //已逾期跟进数量
        $end_count = (new \app\common\model\presalesCustomer\PresalesCustomer())
            ->where($where2)->count();
        return [
            'today_count' => $today_count,
            'end_count' => $end_count,
        ];
    }
}
